//
//  TabBarController.m
//  weibo
//
//  Created by sangcixiang on 17/1/2.
//  Copyright © 2017年 sangcixiang. All rights reserved.
//

#import "TabBarController.h"
#import "NavigationController.h"
#import "HomeViewController.h"
#import "CategoryControllerView.h"
#import "MessagesController.h"
#import "MeViewController.h"
@interface TabBarController ()<UITabBarControllerDelegate>
@end

@implementation TabBarController

+(void)initialize{
    /** 设置tabbar字体颜色 */
    NSDictionary *dictNormal = @{NSForegroundColorAttributeName:HexRGB(0x808080),NSFontAttributeName:[UIFont systemFontOfSize:12]};
    [[UITabBarItem appearance] setTitleTextAttributes:dictNormal forState:UIControlStateNormal];
    NSDictionary *dictSelected = @{NSForegroundColorAttributeName:BaseThemeColor,NSFontAttributeName:[UIFont systemFontOfSize:12]};
    [[UITabBarItem appearance] setTitleTextAttributes:dictSelected forState:UIControlStateSelected];
    [[UITabBar appearance]  setTranslucent:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addConroller];
    self.delegate = self;
}

-(void)addConroller{
    
    NavigationController *homeNav = [self addController:[HomeViewController new] withTitle:@"首页" withImage:@"home" withSelectImage:@"home_select"];
    
    NavigationController *allNav = [self addController:[CategoryControllerView new] withTitle:@"分类" withImage:@"assortment" withSelectImage:@"assortment_select"];

    NavigationController *msgNav = [self addController:[MessagesController new] withTitle:@"消息" withImage:@"message" withSelectImage:@"message_select"];
    
    NavigationController *myNav = [self addController:[MeViewController new] withTitle:@"我的" withImage:@"me" withSelectImage:@"me_select"];
    
    self.viewControllers = @[homeNav,allNav,msgNav,myNav];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    
    UINavigationController *nav = (UINavigationController *)viewController;
    UIViewController *VC =nav.topViewController;
    if ([VC isKindOfClass:[MessagesController class]]) {
        XLUserModel *user = [XLUserModel getUserInfo];
        if (!user) {
            NavigationController *currentNav = self.selectedViewController;
            UIViewController *login = SBView(@"Login", @"LoginViewController");
            [currentNav pushViewController:login animated:YES];
            return NO;
        }else{
            return YES;
        }
    }
    return YES;
    
}

-(NavigationController *)addController:(UIViewController *)controller withTitle:(NSString *)title withImage:(NSString *)imageNmae withSelectImage:(NSString *)selectImageName{
    controller.title = title;
    controller.tabBarItem.title = title;
    controller.tabBarItem.image = [self imageName:imageNmae];
    controller.tabBarItem.selectedImage = [self imageName:selectImageName];
    return [[NavigationController alloc]initWithRootViewController:controller];;
    
}

-(UIImage *)imageName:(NSString *)image{
    
    UIImage *OriginalImage = [UIImage imageNamed:image];
    OriginalImage = [OriginalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    return OriginalImage;
}

-(void)dealloc{
    NSLog(@"%s",__func__);
}











@end
